const app = getApp()
Page({
  data:{
    nickName:"",
    avatarUrl:"",
  },
 async user(){
  let res =await wx.getUserProfile({
    desc:'授权了才能使用',
  })
  let {nickName,avatarUrl} = res.userInfo
  this.setData({
    nickName,avatarUrl
  })
},
async submit(e){
  console.log(e)
  let {sn,name} = e.detail.value
  if(!this.data.avatarUrl){
    wx.showToast({
      title:'请获取微信账号信息',
    })
    return
  }
  if(!sn|!name){
    wx.showToast({
      title: '请输入学号姓名',
    })
    return
  }
  if(!/^\d{8}$/.test(sn)){
    wx.showToast({
      title: '请输入8位学号',
      icon:'error'
    })
    return
  }
  let {nickName,avatarUrl} = this.data
  let res = await wx.cloud.callFunction({
    name:'pc_register',
    data:{
      sn,name,nickName,avatarUrl
    }
  })
  console.log(res)
  if(res.result.reg=='ok'){
    wx.showToast({
      title: '绑定成功',
    })
    app.globalData.user = res.result.user
    wx.navigateTo({
      url: '/pages/work18/rank/rank',
    })
  }else{
    wx.showModal({
      cancelColor: 'cancelColor',
      icon:'err',
      title:res.result.reg,
      content:res.result.errMsg
    })
  }
}
})